package com.cskaoyan.service.impl;

import com.cskaoyan.bean.bo.AdListBO;
import com.cskaoyan.bean.pojo.MarketAd;
import com.cskaoyan.bean.pojo.MarketAdExample;
import com.cskaoyan.bean.vo.AdListVO;
import com.cskaoyan.mapper.MarketAdMapper;
import com.cskaoyan.service.AdService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 广告管理
 *
 * @author chenxu
 * @since 2022/04/06 16:35
 */
@Service
public class AdServiceImpl implements AdService {
    @Autowired
    MarketAdMapper adMapper;
    /*
     *
     *获取页面的广告信息
 * @param adListBO
     * @return com.cskaoyan.bean.vo.AdListVO
     * @author chenxu
     * @since 2022/04/06 20:37
     */
    @Override
    public AdListVO list(AdListBO adListBO) {
        //当前页码
        Integer page = adListBO.getPage();
        //数据量
        Integer limit = adListBO.getLimit();
        //desc\asc
        String desc = adListBO.getOrder();
        //排序列
        String sort = adListBO.getSort();
        PageHelper.startPage(page,limit);
        //创建一个example的对象
        MarketAdExample example = new MarketAdExample();
        //使用createCriteriteria方法创建exaple的内部类对象，里面维护了条件列表
        MarketAdExample.Criteria criteria = example.createCriteria();
        example.setOrderByClause(adListBO.getSort()+" "+adListBO.getOrder());
        //根据需求提供对应的条件----使用对应的方法
        //考虑到条件查询中name和content为null的条件
        if (adListBO.getName()!=null){
            criteria.andNameLike("%"+adListBO.getName()+"%");}
        if (adListBO.getContent()!=null){
            criteria .andContentLike("%"+adListBO.getContent()+"%");
        }
        criteria.andDeletedEqualTo(false);
        List<MarketAd> marketAds = adMapper.selectByExample(example);
       //new一个AdlistVO，把查询到的list set进去。
        PageInfo<MarketAd> pageInfo = new PageInfo<>(marketAds);
        int pages = pageInfo.getPages();
        int total = (int) pageInfo.getTotal();
        AdListVO adListVO = new AdListVO();
        adListVO.setLimit(limit);
        adListVO.setList(marketAds);
        adListVO.setPage(page);
        adListVO.setPages(pages);
        adListVO.setTotal(total);
        return adListVO;
    }

    /*
     *
     * 删除广告信息
 * @param id
     * @return int
     * @author chenxu
     * @since 2022/04/06 20:38
     */
    @Override
    public void delete(MarketAd marketAd) {
        marketAd.setDeleted(true);
        adMapper.updateByPrimaryKey(marketAd);
    }

    /*
     *
     * 修改广告信息
 * @param marketAd
     * @return com.cskaoyan.bean.pojo.MarketAd
     * @author chenxu
     * @since 2022/04/06 21:18
     */
    @Transactional
    @Override
    public MarketAd update(MarketAd marketAd) {
        //先修改
        adMapper.updateByPrimaryKey(marketAd);
        //在查找
        MarketAd marketAd1 = adMapper.selectByPrimaryKey(marketAd.getId());
        return marketAd1;
    }

    @Override
    public MarketAd create(MarketAd marketAd) {
        //增加创建时间信息
        marketAd.setDeleted(false);
        marketAd.setAddTime( new Date());
        marketAd.setUpdateTime(new Date());

        adMapper.insert(marketAd);

        return marketAd;
    }

}
